<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2020/1/8
 * Time: 13:55
 */
namespace app\admin\controller;
use app\admin\model\AdminActionLogModel;
use app\common\controller\BackendController;
use app\common\logic\FileLogic;
use app\common\model\FileModel;
use think\Exception;
use think\facade\Db;
use util\ArrayUtil;
use util\FileUtil;

class File extends BackendController
{
    protected $noNeedLogin = ['fileOutput'];
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(){
        if($this->request->isAjax()){
            $this->model = FileModel::instance();
            list($where,$sort,$order,$page,$limit) = $this->model->buildParams($this->request);
            $total = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page,$limit)
                ->order($sort,$order)
                ->append(['type_text'])
                ->select();
            $this->success('查询成功','',['list'=>$list,'total'=>$total]);
        }
        return view();
    }

    public function add(){
        if($this->request->isPost()){
            $this->success('成功');
        }
        return view('edit');
    }
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $ids = $this->request->param('ids/a');
        if(empty($ids)){
            $this->error('删除的数据不能为空！');
        }
        $file_list = FileModel::select($ids);
        try {
            Db::transaction(function () use ($ids,$file_list) {
                $path_list = ArrayUtil::instance()->array_extract($file_list,'full_path');
                foreach ($path_list as $path){
                    if(!unlink($path)){
                        throw new Exception('删除文件失败,文件路径：'.$path,402);
                    }
                }
                $res = FileModel::destroy($ids,true);
                if (!$res) {
                    throw new Exception('删除失败！', 401);
                }
            });
            AdminActionLogModel::logAdd('删除附件','附件ids:'.json_encode($ids));
            $this->success('删除成功');
        }catch (Exception $e){
            $this->error('删除失败,'.$e->getMessage());
        }
    }
    public function pictureUpload(){
        $data = FileLogic::instance()->pictureUpload();
        if(is_array($data) || is_object($data)){
            $this->success('上传成功',null,$data);
        }
        $this->error($data);
    }

    public function layeditPictureUpload(){
        $data = FileLogic::instance()->pictureUpload('file');
        if(is_array($data) || is_object($data)){
            $result = [
                'src'=>getFileUrl($data['path']),
                'title'=>$data['name']
            ];
            $this->error('上传成功',null,$result);//layui的编辑器0表示成功 是反的
        }
        $this->success($data);
    }

    public function FileUpload(){
        $data = FileLogic::instance()->fileUpload();
        if(is_array($data) || is_object($data)){
            $this->success('上传成功',null,$data);
        }
        $this->error($data);
    }

    public function checkPictureExists(){
        $file_info = FileLogic::instance()->checkPictureExists(['md5'=>$this->request->param('md5')]);
        if($file_info){
            $this->error('图片已存在',null,$file_info);
        }
        $this->success('图片不存在，可以上传');
    }
    public function checkFileExists(){
        $file_info = FileLogic::instance()->checkFileExists(['md5'=>$this->request->param('md5')]);
        if($file_info){
            $this->error('文件已存在',null,$file_info);
        }
        $this->success('文件不存在，可以上传');
    }

    public function fileOutput(){
        $full_path = $this->request->param('full_path');
        if(empty($full_path)){
            $this->error('文件路径不能为空');
        }
        if(!file_exists($full_path)){
            $this->error('文件不存在');
        }
        FileUtil::instance()->file_put_out($full_path,false);
    }
}